export default {
    async addUserLiked({ data }) {
        const db = this.database();
        const col = db.collection('user_liked');

        const likedRes = await col.where({
            itemId: data.itemId
        }).get()

        let isLiked = likedRes.data.length > 0 ? true : false



        if (isLiked) {
            const target = likedRes.data[0]
            const removeRes = await col.doc(target._id).remove()
            return {
                ...removeRes,
                message: '取消点赞'
            }
        }
        const req = {
            ...data,
            viewStamp: new Date().getTime(),
        }

        console.log('add历史记录', req, likedRes);


        let res = col.add(req)
        return {
            ...res,
            message: '点赞成功'
        }
    },

    async getUserLiked() {
        const db = this.database();
        const col = db.collection('user_liked');
        const res = await col.orderBy("viewStamp", "desc").get()

        return res.data
    }
}